from appserver import db_store
from sqlalchemy.orm import relationship, backref

class Area(db_store.Model):
    __tablename__ = "areas"
    __bind_key__ = "main"
    id = db_store.Column(db_store.Integer, primary_key=True, comment=u'地区id')
    name = short_name = db_store.Column(db_store.String(50),nullable=False,comment=u'名字')  #名字
    parent_id = db_store.Column(db_store.Integer,default=0,nullable=False)
    short_name = db_store.Column(db_store.String(50),nullable=False,comment=u'简称')   #简称
    lng = db_store.Column(db_store.Float,nullable=False,comment=u'经度')  # 经度
    lat = db_store.Column(db_store.Float,nullable=False,comment=u'纬度')   # 纬度
    level = db_store.Column(db_store.Integer,nullable=False,comment=u'等级')   # 等级(1省/直辖市,2地级市,3区县,4镇/街道)
    dis_order = db_store.Column(db_store.Integer,nullable=False,comment=u'排序')   #排序
    status = db_store.Column(db_store.Integer,nullable=False,comment=u'状态')   #状态(0禁用/1启用)

    def __repr__(self):
        return '<Area %r>' %self.name

    @staticmethod
    def get_area_all():
        return Area.query.all()

    @staticmethod
    def get_areas_by_level(level):
        return Area.query.filter_by(level=level).order_by("dis_order").all()

    @staticmethod
    def get_areas_by_parent_id(parent_id):
        return Area.query.filter_by(parent_id=parent_id).order_by("dis_order").all()
